package org.stormphoenix.bbsfamily.utils

import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.ObjectMapper

object JsonUtil {
    private val MAPPER = ObjectMapper()

    fun objectToJson(data: Any): String? {
        try {
            return MAPPER.writeValueAsString(data)
        } catch (e: JsonProcessingException) {
            e.printStackTrace()
        }

        return null
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     */
    fun <T> jsonToPojo(jsonData: String, beanType: Class<T>): T? {
        try {
            return MAPPER.readValue(jsonData, beanType)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return null
    }
}
